package com.kuaimacode.kframework.api.service.category;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.category.HotGoodsMapper;
import com.kuaimacode.kframework.mybatis.models.category.HotGoods;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author Administrator
 * @date 2019/03/24
 */
@Service
public class HotGoodsService extends ServiceImpl<HotGoodsMapper, HotGoods> {

    	/**
    	 *  分页查找所有的数据
    	 * @param pageNum 当前页码，从1开始
    	 * @param pageSize  一页显示多少行
    	 * @return
    	 */
    	public PageInfo<HotGoods> findAll(int pageNum, int pageSize,String agentId) {
			QueryWrapper queryWrapper = new QueryWrapper();
    		if(agentId!=null&&!"".equals(agentId)){
				queryWrapper.eq("agent_id",agentId);
			}else{
    			queryWrapper.eq("agent_id", Constant.DEFAULT_AGENT_ID);
			}
    		PageHelper.startPage(pageNum, pageSize,true,false,null);
    		List<HotGoods> datas = baseMapper.selectList(queryWrapper);
    		return new PageInfo<HotGoods>(datas);
    	}

	public PageInfo<HotGoods> findByCategory(int pageNum, int pageSize,String agentId,String categoryId) {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("category_id",categoryId);
		queryWrapper.eq("del_state",Constant.DeleteStateEnum.NORMAL.getCode());
		PageHelper.startPage(pageNum, pageSize,true,false,null);
		List<HotGoods> datas = baseMapper.selectList(queryWrapper);
		if(datas==null||datas.size()<1){//如果该品类下无法查询的热门商品，取通用的热门商品
			QueryWrapper queryWrapper1 = new QueryWrapper();
			queryWrapper1.eq("agent_id",Constant.DEFAULT_AGENT_ID);
			queryWrapper.eq("del_state",Constant.DeleteStateEnum.NORMAL.getCode());
			PageHelper.startPage(pageNum, pageSize,true,false,null);
			datas = baseMapper.selectList(queryWrapper1);
		}

		return new PageInfo<HotGoods>(datas);
	}
}

